<!DOCTYPE html><html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="Author" />
    <title>查看告警</title>
    <style type="text/css">
        table { width: 85%; }
        th,td {
                    word-wrap: break-word;
                    overflow-wrap: break-word;
                    white-space: normal;
                    max-width: 800px;
                    min-width: 100px;
        }
    </style>
    <script>
        let header = [
            {'code':'appName', 'title':'应用名称'},
            {'code':'level', 'title':'告警等级'},
            {'code':'name', 'title':'告警名称'},
            {'code':'date', 'title':'告警时间'},
            {'code':'desc', 'title':'告警描述'}
        ];
        let removeSuffs = [/监控告警$/g
				,/统一监控平台$/g
				,/\d{2}:\d{2}$/g
				,/[昨今]天$/g
				,/\d{1,2}月\d{1,2}日$/g
				,/聊天内有较多未读消息， 点此生成智能摘要$/g
        ];
		let contentPres = ['告警描述'
				,'告警内容'
		];

        <!--核心告警配置-->
        let coreAlarmSplitWord ='@机器人';
        let coreSysNames = ['清结算系统'
                        ,'收费分润系统'
                        ,'新核心清分系统'
                        <!-- ,'对账系统'-->
                        <!-- ,'资损防控'-->
						];
        let coreHits = [
            {'level':'一般严重', 'desc':'!告警已恢复'}
            ,{'name':'重试失败', 'desc':'!告警已恢复'}
            ,{'name':'清分重试执行次数超限', 'desc':'!告警已恢复'}
            ,{'name':'创建结算单异常', 'desc':'!告警已恢复'}
        ];

        <!--对账告警配置-->
        let reconAlarmSplitWord ='@机器人';
        let reconHits = [
            {'name':'分分卡对账文件缺失'},
            {'level':'一般严重', 'desc':'!告警已恢复'}
        ];

        <!--车贷告警配置-->
        let floodAlarmSplitWord =/(统一监控平台: )|(公共告警: )/;
        let floodHits = [
            {'name':'贷款申请超过30天连登手机号为空', 'desc':'!告警已恢复'}
        ];

        <!--运维告警配置-->
        let opAlarmSplitWord ='@机器人';
        let opSysNames = ['清结算系统'
                        ,'收费分润系统'
                        ,'新核心清分系统'
                        <!-- ,'对账系统'-->
                        <!-- ,'资损防控'-->
						];
        let opHits = [
            {'name':'SQL执行超过'}
        ];



        window.onload = function() {
<!--            textarea分段；构建对象，按应用名称汇总list；输出到表格-->
<!--                    日志告警-->
<!--                    数据中心：水土数据中心-->
<!--                    所属战队：金融能力-公共战队(未定义)-->
<!--                    系统名称：统一对账系统-->
<!--                    应用名称：资损防控(李勇)-->
<!--                    告警等级：警告-->
<!--                    告警名称：规则计算异常监控-->
<!--                    告警时间：2025-01-01 02:31-->
<!--                    告警描述：资损防控_日志告警_规则计算异常-->
            document.getElementById("handleCoreAlarm").addEventListener('click', handleCoreAlarm);
            document.getElementById("handleReconAlarm").addEventListener('click', handleReconAlarm);
            document.getElementById("handleFloodAlarm").addEventListener('click', handleFloodAlarm);
            document.getElementById("handleOpAlarm").addEventListener('click', handleOpAlarm);

        };

        function handleCoreAlarm() {
            let infos = document.getElementById("info").value.split(coreAlarmSplitWord);
            let html ='';
            for(let i=0; i<coreSysNames.length; i++){
                let alarms = [];
                for(let j=0; j<infos.length; j++){
                    let str = infos[j];
                    if(str.includes(coreSysNames[i])&&str&&str.length>20){
						let alarm=lineToAlarm(str);
						if(alarm.appName.includes(coreSysNames[i]))
							alarms.push(alarm);
                    }
                }
                if(alarms.length>0){
                    html +=`<h2>${coreSysNames[i]}</h2>\n`;
                    html +=info2Html(alarms, header, coreHits);
                }
            }
            //清空之前的数据
            document.getElementById("table_div").innerHTML='';
            console.log(html);
            document.getElementById("table_div").innerHTML=html;
        }

        function lineToAlarm(str){
            let lines = str.split(/[\r\n]+/);
            let alarm={};
            alarm.type=lines[0];
            for(let k=1; k<lines.length; k++){
                let line=lines[k].trim();
                let value=line.substring(line.indexOf('：')+1);
                if(line.startsWith('数据中心')){
                    alarm.dateCenter=value;
                }else if(line.startsWith('所属战队')){
                    alarm.team=value;
                }else if(line.startsWith('系统名称')){
                    alarm.sysName=value;
                }else if(line.startsWith('应用名称')){
                    alarm.appName=value;
                }else if(line.startsWith('告警等级')){
                    alarm.level=value;
                }else if(line.startsWith('告警名称')){
                    alarm.name=value;
                }else if(line.startsWith('告警时间')){
                    alarm.date=value;
                }
                for(let i=0; i<contentPres.length; i++){
                    if(line.startsWith(contentPres[i])){
                        let desc = str.substring(str.indexOf(line))
                            .substring(contentPres[i].length+1);
                        for(let j=1; j<removeSuffs.length; j++){
                            desc = desc.trim()
                                .replaceAll(removeSuffs[j], '');
                        }
                        alarm.desc=desc;
                        break;
                    }
                }
            }
            return alarm;
        }

        function info2Html(jsonArr, header, hitObjs){
            let tdCodes = [];
            let tableHeader = header.map(function(val, index, arr){
                tdCodes.push(val.code);
                return `<th>${val.title}</th>`;
            }).join("\n");
            tableHeader = `<tr>${tableHeader}</tr>`;
            jsonArr.sort((a,b)=>a.date.localeCompare(b.date));
            let tableBody = jsonArr.map(function(val, index, arr){
                if(!val || !val.appName){
                    return '';
                }
                let temp = tdCodes.map(function(tdCode, index, arr){
                    let tdInfo=val[tdCode];
                    if(tdCode=='desc'&&tdInfo.length>100){
                        return `<td><a href="#" title="${tdInfo}">${tdInfo.substring(0,100)}</a>></td>`;
                    }
                    return `<td>${tdInfo}</td>`;
                }).join("\n");

                let flag=false;
                for(let i=0; hitObjs&&i<hitObjs.length; i++){
                    let hitObj=hitObjs[i];
                    for (let key in hitObj) {
                        if (hitObj.hasOwnProperty(key)) {
                            let hitVal = hitObj[key];
                            if((hitVal.startsWith('!')&&!val[key].includes(hitVal.substring(1)))
                            || (!hitVal.startsWith('!')&&val[key].includes(hitVal))){
                                flag=true;
                            }else{
                                flag=false;
                                break;
                            }
                        }
                    }
                    if(flag){
                        break;
                    }
                }
                return flag ? `<tr style="background:yellow">${temp}</tr>`:`<tr>${temp}</tr>`;
            }).join("\n");
            return `<table border="1">${tableHeader}${tableBody}</table>\n`;
        }

        function handleReconAlarm() {
            let infos = document.getElementById("info").value.split(reconAlarmSplitWord);
            let html ='';

            let alarms = [];
            for(let j=0; j<infos.length; j++){
                let str = infos[j];
                if(str&&str.length>20){
                    alarms.push(lineToAlarm(str));
                }
            }
            if(alarms.length>0){
                html +=`<h2>对账清算告警</h2>\n`;
                html +=info2Html(alarms, header,reconHits);
            }
            //清空之前的数据
            document.getElementById("table_div").innerHTML='';
            console.log(html);
            document.getElementById("table_div").innerHTML=html;
        };

        function handleFloodAlarm() {
            contentPres = ['告警描述'
                    ,'告警内容'
                    ,'内容'
            ];
            let infos = document.getElementById("info").value.split(floodAlarmSplitWord);
            let html ='';

            let alarms = [];
            for(let j=0; j<infos.length; j++){
                let str = infos[j];
                if(str&&str.length>20){
                    alarms.push(lineToAlarm(str));
                }
            }
            if(alarms.length>0){
                html +=`<h2>车贷告警</h2>\n`;
                html +=info2Html(alarms, header,floodHits);
            }
            //清空之前的数据
            document.getElementById("table_div").innerHTML='';
            console.log(html);
            document.getElementById("table_div").innerHTML=html;
        };


        function handleOpAlarm() {
            let infos = document.getElementById("info").value.split(opAlarmSplitWord);
            let html ='';
            for(let i=0; i<opSysNames.length; i++){
                let alarms = [];
                for(let j=0; j<infos.length; j++){
                    let str = infos[j];
                    if(str.includes(coreSysNames[i])){
						let alarm=lineToAlarm(str);
						if(alarm.appName.includes(coreSysNames[i])&&str&&str.length>20)
							alarms.push(alarm);
                    }
                }
                if(alarms.length>0){
                    html +=`<h2>${coreSysNames[i]}</h2>\n`;
                    html +=info2Html(alarms, header, opHits);
                }
            }
            //清空之前的数据
            document.getElementById("table_div").innerHTML='';
            console.log(html);
            document.getElementById("table_div").innerHTML=html;
        }
    </script>
</head>
<body>

<div>
    <textarea rows="20" cols="120" id="info" name="info" placeholder="告警内容" ></textarea>
    <br/>
    <input type="button" id="handleCoreAlarm" value="核心告警"/>
    <input type="button" id="handleReconAlarm" value="对账告警"/>
    <input type="button" id="handleFloodAlarm" value="车贷告警"/>
    <input type="button" id="handleOpAlarm" value="运维告警"/>
</div>
<div id="table_div"></div>



</body>
</html>
